import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SpPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _Sp();
  }
}

class _Sp extends State<SpPage> {
  final _userNameController = new TextEditingController();
  final _userPwdController = new TextEditingController();

  save() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString("userName", _userNameController.value.text.toString());
  }

  Future<String> get() async {
    var userName;
    SharedPreferences prefs = await SharedPreferences.getInstance();
    userName = prefs.getString("userName");
    return userName;
  }

  @override
  void initState() {
    super.initState();
    get().then((text) {
      _userNameController.text = text;
      print(text);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        //leading: Icon(Icons.arrow_back),
        title: Text("shared_preferences"),
        centerTitle: true,
      ),
      body: Column(
        children: <Widget>[
          TextField(
            controller: _userNameController,
            maxLines: 1,
            decoration: InputDecoration(
              contentPadding: const EdgeInsets.only(top: 10.0),
              icon: Icon(Icons.person),
              labelText: "请输入用户名",
              helperText: "注册时填写的名字",
            ),
          ),
          TextField(
            controller: _userPwdController,
            maxLines: 1,
            decoration: InputDecoration(
              contentPadding: const EdgeInsets.only(top: 10.0),
              icon: Icon(Icons.keyboard),
              labelText: "请输入密码",
              helperText: "注册时填写的密码",
            ),
          ),
          Builder(builder: (ctx) {
            return RaisedButton(
                child: Text("登录"),
                onPressed: () {
                  if (_userNameController.text.toString().isEmpty) {
                    Scaffold.of(ctx).showSnackBar(SnackBar(
                      content: Text("请输入用户名"),
                      duration: Duration(seconds: 1),
                    ));
                    return;
                  }
                  if (_userPwdController.text.toString().isEmpty) {
                    Scaffold.of(ctx).showSnackBar(SnackBar(
                      content: Text("请输入密码"),
                      duration: Duration(seconds: 1),
                    ));
                    return;
                  }
                  if (_userPwdController.text.toString() == "123456") {
                    Scaffold.of(ctx).showSnackBar(SnackBar(
                      content: Text("登录成功"),
                      duration: Duration(seconds: 1),
                    ));
                    save();
                  }else{
                    Scaffold.of(ctx).showSnackBar(SnackBar(
                      content: Text("登录失败"),
                      duration: Duration(seconds: 1),
                    ));
                  }
                });
          })
        ],
      ),
    );
  }
}
